const positionCenter = { lon: 108, lat: 30, height: 1000 }; // 中心点
const positionCenter2 = { lon: 107.5, lat: 29.5, height: 1000 }; // 中心点
// 中心点到四周点的步长
const step = {
  lonStep: 0.1,
  latStep: 0.1,
};

// 获取中心点的四周点位信息
const getPositions = (center, size) => {
  if (!center) return;
  const positions = [];
  const type = [1, -1];
  const { lon, lat, height } = center;
  const { lonStep, latStep } = size;
  type.forEach((attr) => {
    type.forEach((item) => {
      const position = {
        lon: lon + lonStep * attr,
        lat: lat + latStep * item,
        height,
      };
      const cartesian = Cesium.Cartesian3.fromDegrees(
        position.lon,
        position.lat,
        position.height
      );
      positions.push(cartesian);
    });
  });
  [positions[3], positions[2]] = [positions[2], positions[3]];
  return positions;
};

// 添加贴地图片
const addImage = () => {
  viewer.entities.add({
    polygon: {
      hierarchy: getPositions(positionCenter, step),
      material: new Cesium.ImageMaterialProperty({
        image: "../img.png",
      }),
      perPositionHeight: false,
    },
  });
};

addImage();

// 添加贴地视频
const addVideo = () => {
  const video = document.getElementById("video");
  const entity = viewer.entities.add({
    polygon: {
      hierarchy: getPositions(positionCenter2, step),
      material: video,
      perPositionHeight: false,
    },
  });
  viewer.trackedEntity = entity;
};

addVideo();

// 添加图层
const addLayer = () => {
  viewer.imageryLayers.addImageryProvider(
    new Cesium.SingleTileImageryProvider({
      url: "../img.png",
      rectangle: Cesium.Rectangle.fromDegrees(106, 29, 107, 30),
      tileWidth: 100,
      tileHeight: 100,
    })
  );
};

addLayer();
